package com.hsjk.szwj.module.pay.service;

import com.hsjk.szwj.framework.common.exception.ServiceException;
import com.hsjk.szwj.framework.common.exception.enums.GlobalErrorCodeConstants;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.Set;

/**
 * @author chenjiawei
 * @desc
 * @Date 2023/08/21 15:56
 **/
@Service
public class ValidateService {
    @Resource
    private Validator validator;

    public void validate(Object obj) {

        Set<ConstraintViolation<Object>> resultSet = validator.validate(obj);
        if (resultSet == null || resultSet.isEmpty()) {
            return;
        }
        resultSet.stream().forEach(item -> {
            throw new ServiceException(GlobalErrorCodeConstants.BAD_REQUEST.getCode(),
                    item.getMessage());
        });
    }
}
